package com.huang.springboot.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;

/**
 * Druid配置类
 * @author yili_gi
 */
@Configuration
public class DruidConfig {

    /**
     *加载数据源到spring的容器中，引用配置文件的配置
     * @return
     */
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }

    /**
     * 配置StatViewServlet
     * @return
     */
    @Bean
    public ServletRegistrationBean<StatViewServlet> statViewServlet(){

        ServletRegistrationBean<StatViewServlet> bean =
                new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        HashMap<String,String> initParameters = new HashMap<>(10);
        initParameters.put("loginUsername","admin");
        initParameters.put("loginPassword","123456");
        //访问白名单
        initParameters.put("localhost","127.0.0.1");
        //访问黑名单
        initParameters.put("someone","168.0.1.123");
        bean.setInitParameters(initParameters);
        return bean;

    }

}
